--[[
表格ui
用表名label+切页按钮+列表组成
]]
TableEvent = RegisterEvents{
    "SelectedTableLine"
}
Table = {
    props = {
        _pageItems = 5,
        _pageIndex = 1,
    },
    gets = {
        pageItems = function (self)
            return self._pageItems
        end,
        pageIndex = function (self)
            return self._pageIndex
        end,
        nextPageButton = function (self)
            return self._nextPageButton
        end,
        lastPageButton = function (self)
            return self._lastPageButton
        end,
        tableList = function (self)
            return self._tableList
        end
    },
    sets = {
        pageItems = function (self, key, val)
            self._pageItems = val
            self:refresh()
        end,
        pageIndex = function (self, key, val)
            self._pageIndex = val
            self:refresh()
        end,
        nextPageButton = function (self, key, val)
            if self._nextPageButton then
                self._nextPageButton:offClick(self.nextPage, self)
            end
            self._nextPageButton = val
            val:onClick(self.nextPage, self)
        end,
        lastPageButton = function (self, key, val)
            if self._lastPageButton then
                self._lastPageButton:offClick(self.lastPage, self)
            end
            self._lastPageButton = val
            val:onClick(self.lastPage, self)
        end,
        selectedTableLine = function (self, key, val)
            if self._selectedTableLine then
                self._selectedTableLine:switch(false)
            end
            self._selectedTableLine = val
            self:emit(TableEvent.SelectedTableLine, val)
        end,
        tableList = function (self, key, val)
            self._tableList = val
            val.listenItemClick = true
            val:onListItemClick(function (item, index)
                self.selectedTableLine = item
            end)
        end
    }
}

function Table:ctor(params)
    self._pageItems = params.pageItems or 5
    if params.tableItemsGetter then
        self.tableItemsGetter = params.tableItemsGetter
    end
    if params.generateTableLineData then
        self.generateTableLineData = params.generateTableLineData
    end
end

function Table:tableItemsGetter()
    return #self.tableParams.data
end

function Table:generateTablePageData()
    local start = (self.pageIndex - 1) * self.pageItems + 1
    local nums = self:tableItemsGetter() - start
    if nums > self.pageItems then
        nums = self.pageItems
    end

    local pageData = {}
    for i = start, start + nums - 1 do
        table.insert(pageData, self:generateTableLineData(i))
    end
    return pageData
end

function Table:generateTableLineData(i)
    return self.tableParams.data[i]
end
function Table:refresh()
    local pageData = self:generateTablePageData()
    self.tableList:setListData(pageData)
end

function Table:nextPage()
    local pageMax = math.ceil(self:tableItemsGetter() / self.pageItems)
    self.pageIndex = math.min(self.pageIndex + 1, pageMax)
end

function Table:lastPage()
    self.pageIndex = math.max(self.pageIndex - 1, 1)
end

function Node.Table(params)
    local node = Node.Layout()
    node.name = "table"
    node:addComponent(Stretch)
    node.fillLayout = true
    node:addComponent(Table, params)
    node.tableParams = params
    local pool = ObjectPool.new(function (itemData)
        local line = Node.TableLine(params.headers)
        line.height = 40
        return line
    end)
    if params and params.name then
        Node.Label(params.name):addTo(node)
    end

    local btnLayout = Node.Layout():addTo(node)
    btnLayout.layoutType = LayoutType.Horizontal
    btnLayout.layoutSizeMode = LayoutSizeMode.Stretch
    btnLayout.name = "btnLayout"
    node.nextPageButton = Node.Button("下一页"):addTo(btnLayout):wh(70, 30)
    node.lastPageButton = Node.Button("上一页"):addTo(btnLayout):wh(70, 30)

    local header = Node.TableHeader(params.headers):addTo(node)
    -- header.name = "headers"

    local scroll = Node.ScrollList(pool)
    scroll.name = "tableScroll"
    scroll:addComponent(Stretch)
    scroll.fillLayout = true
    scroll:addTo(node)
    node.tableList = scroll.list
    node.scroll = scroll
    node:refresh()
    return node
end